//
//  MyRunDetailViewController.m
//  享动
//
//  Created by darker on 16/4/19.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MyRunDetailViewController.h"

@interface MyRunDetailViewController () <UIWebViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIActivityIndicatorView *activity;

// 没有网络的提示框
@property (nonatomic, strong)  UIView *hint;
@end

@implementation MyRunDetailViewController


- (void)loadData
{
     NSString *url = [NSString stringWithFormat:@"http://ibaby.ipadown.com/api/food/food.show.detail.php?id=%@", _ID];
    [RequestManager requestWithUrl:url requestType:RequestTypePost parDic:nil finish:^(NSData *data) {
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        
        NSLog(@"%@", string);
        dispatch_async(dispatch_get_main_queue(), ^{
            [_activity startAnimating];
            
            NSData *html = data;
            
            NSString *string = [[NSString alloc]initWithData:html encoding:NSUTF8StringEncoding];
            NSLog(@"%@", string);
            
            NSString *stringTemp2 = [NSString stringWithFormat:@"<a href=\"bookmark://\" id=\"bookmark\" class=\"btnfav r3 mt15 ml10\">+收藏</a>"];
            NSString *stringTemp = @"";
            NSString *stringTemp1 = [string stringByReplacingOccurrencesOfString:stringTemp2 withString:stringTemp];
            
            NSRange range1 = [string rangeOfString:@"<script type=\"text/javascript"];
            NSRange rang2 = [string rangeOfString:@"javascript\"></script"];
            NSInteger a = range1.location + 5;
            NSInteger b = rang2.location - range1.location + 33 + 5;
            NSRange rang3 = NSMakeRange(a, b);
            
            NSString *stringTemp4 = [stringTemp1 stringByReplacingCharactersInRange:rang3 withString:stringTemp];
            
            NSString *stringTemp3 = [NSString stringWithFormat:@"<head><style>img{width:%fpx !important;}</style></head>%@",KWIDTH - 50, stringTemp4];
            
            [_webView loadHTMLString:stringTemp3 baseURL:[NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath]];
            
            
            
        });
    } error:^(NSError *error) {
        NSLog(@"错误");
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    [_activity setCenter:CGPointMake(KWIDTH / 2, KHEIGHT / 2 - 30)];
    
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT + 49)];
    
    _webView.scrollView.showsHorizontalScrollIndicator = NO;
    _webView.scrollView.showsVerticalScrollIndicator = NO;
    _webView.backgroundColor = [UIColor whiteColor];
    _scrollView = [_webView.subviews objectAtIndex:0];
    _scrollView.delegate = self;
    
    _webView.delegate = self;
    _webView.scalesPageToFit = NO;
    
    [self.view addSubview:_webView];
    [_webView addSubview:_activity];
    
    
    // 判断网络
    if ([[NetWorkState shareInstance]reachability]) {
        [self loadData];
    } else {
        [self addHint];
    }
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;

}

- (void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    if (point.x > 0)
    {
        scrollView.contentOffset = CGPointMake(0, point.y);
    }
}

// 加载成功
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_activity stopAnimating];
    
}

// 加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
    [self addHint];
}

#pragma mark ---- 没有网络的提示框 ----
- (void)addHint
{
    // 没有网络的提示框
    _hint = [[UIView alloc]initWithFrame:CGRectMake(KWIDTH / 4, KHEIGHT / 2 - KHEIGHT / 15, KWIDTH / 2, KHEIGHT / 15 * 2)];
    _hint.backgroundColor = [UIColor blackColor];
    _hint.alpha = 0.7;
    _hint.layer.cornerRadius = 10;
    [self.view addSubview:_hint];
    
    UIImageView *noNetworkimageView = [[UIImageView alloc]initWithFrame:CGRectMake(_hint.frame.size.width / 3 + 3, 0, _hint.frame.size.width / 3 + 3, _hint.frame.size.width / 3 * 53 / 63)];
    noNetworkimageView.image = [UIImage imageNamed:@"noNetwork"];
    [_hint addSubview:noNetworkimageView];
    
    UILabel *hintLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _hint.frame.size.height / 2, KWIDTH / 2, _hint.frame.size.height / 2)];
    hintLabel.text = @"网络超时";
    hintLabel.textAlignment = NSTextAlignmentCenter;
    hintLabel.textColor = [UIColor whiteColor];
    [_hint addSubview:hintLabel];
    
    [self performSelector:@selector(removeHint) withObject:nil afterDelay:3];
    
}

- (void)removeHint
{
    [_hint removeFromSuperview];
}

- (void) viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void) viewWillDisappear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
